package view.table;

import javax.swing.table.AbstractTableModel;

public class ExchangerTableModel extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Object[][] data;
	private String[] title;

	/**
	 * Constructeur
	 * 
	 * @param data
	 * @param title
	 * @param resourceBundleWrapper 
	 */
	public ExchangerTableModel(Object[][] data, String[] title, String nameFirstCol) {
		this.data = new Object[data.length][5];
		int i =0;
		for(Object[] ob : data){
			this.data[i][0] = new Boolean(false);
			this.data[i][1] = ob[0];
			this.data[i][2] = ob[1];
			this.data[i][3] = ob[2];
			this.data[i][4] = ob[3];
			i++;
		}
		this.title = new String[title.length+1];
		this.title[0] = nameFirstCol;
		i = 1;
		for(String currenttile : title){
			this.title[i] = currenttile;
			i++;
		}
	}
	
	public void update(Object[][] data, String[] title){
		Object[][] currentData = new Object[data.length][5];
		for(int i=0; i<currentData.length; i++){
			if(i<this.data.length)
				currentData[i][0]=this.data[i][0];
			else
				currentData[i][0]=new Boolean(false);
			for(int j=0; j<4; j++){
				currentData[i][j+1]=data[i][j];
			}
		}
		this.data = currentData;
		int i = 1;
		for(String currenttile : title){
			this.title[i] = currenttile;
			i++;
		}
		fireTableStructureChanged();
	}

	/**
	 * Retourne le nom d'une colonne
	 * 
	 * @param col
	 */
	public String getColumnName(int col) {
		return this.title[col];
	}
	
	public Class<? extends Object> getColumnClass(int col){
		return this.data[0][col].getClass();
	}
	

	/**
	 * Retourne le nombre de colonnes
	 */
	public int getColumnCount() {
		return this.title.length;
	}

	/**
	 * Retourne le nombre de lignes
	 */
	public int getRowCount() {
		return this.data.length;
	}

	/**
	 * Retourne la valeur � l'emplacement sp�cifi�
	 */
	public Object getValueAt(int row, int col) {
		return this.data[row][col];
	}
	
	@Override
	public void setValueAt(Object aValue, int row, int column) {
		// TODO Auto-generated method stub
		this.data[row][column] = aValue;
		fireTableCellUpdated(row, column);
	}
	
	public boolean isCellEditable(int row, int col){
		if(col == 4 || col == 0)
			return true;
		else 
			return false;
	}
	
	public void refresh(Object[][] data){
		this.data = new Object[data.length][this.title.length + 1];
		int i = 0;
		for (Object[] o : data) {
			this.data[i][0] = new Boolean(false);
			for (int k = 0; k < 4; k++)
				this.data[i][k + 1] = o[k];
			System.out.println(o[0]);
			i++;
		}
		fireTableStructureChanged();
	}


}
